package com.example.as_sum.activity;

import android.os.Handler;
import android.os.Message;
import android.os.Bundle;
import com.example.as_sum.util.Log;
import com.example.as_sum.R;
import com.example.as_sum.util.StatusBarCompat;
import com.example.as_sum.util.StatusBarCompat2;

import butterknife.ButterKnife;

public abstract class BaseActivity extends RootActivity {

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(getContentId());
        StatusBarCompat.setStatusBarColor(this,getResources().getColor(R.color.colorPrimaryDark));
        ButterKnife.bind(this);
        initView();
        addListener();
        loadData();
        String tga=this.getClass().getSimpleName();
        Log.e(tga,"onCreate");
    }
    public abstract int getContentId();
    public abstract void initView();
    public abstract void addListener();
    public abstract void loadData();
    public abstract Handler getHandler();

    //错误处理
    public void requestFail(Message msg) {
        Bundle b = msg.getData();
        String error = b.getString("error");

        if (msg.arg2 == 1003) {
            //错误码1003
            showLongMsg(error);
        }else if(msg.arg2 == 2003) {
            //错误码2003
            showLongMsg("服务器返回错误");
        }
    }
    @Override
    protected void onDestroy() {
        Handler handler=getHandler();
        if(handler!=null){
            //移除所有的未完成的请求，防止Activity已经销毁，handler还没执行完
            handler.removeCallbacksAndMessages(null);
        }
        ButterKnife.unbind(this);
        super.onDestroy();
    }
}
